<?php

class NewsDetail extends Controller {

	function NewsDetail()
	{
		parent::Controller();
	}

	function index()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
		$this->load->view('header');
		$this->load->view('news-detail-form');
		$this->load->view('footer');
	}
	
	
	function detail()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('NewsModel');
	    $newsID = $this->input->post('txtId'); 
	    if($newsID != null) {
			$data = $this->NewsModel->getNewsByID($newsID);		
			$this->load->view('header');
			$this->load->view('news-detail-form', $data);
			$this->load->view('footer');	    	
	    }     
	}
	
	function delete()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('NewsModel');
	    $newsID = $this->input->post('txtNewsID'); 
	    if($newsID != null) {
			$data = $this->NewsModel->deleteNews($newsID);				
	    }   
	   redirect('NewsList/index');	 	
	}
	
	function upload()
	{
		$this->load->model('NewsModel');
		
		$data = array(
               'title' => $this->input->post('txtTitle') ,
			   'intro' => $this->input->post('txtIntro') ,
               'detail' => $this->input->post('txtDetail') ,
			   'show_main_page' => $this->input->post('chkShow')  ? $this->input->post('chkShow') : 'N',
				'show_edu' => $this->input->post('chkEduShow')  ? $this->input->post('chkEduShow') : 'N',
			   'show_level_anuban' => $this->input->post('chkLevel2'),
			   'show_level_pratom' => $this->input->post('chkLevel3'),
				'show_level_mo_ton' => $this->input->post('chkLevel4'),
				'show_level_mo_pay' => $this->input->post('chkLevel5'),
				'show_level_general' => $this->input->post('chkLevel6'),
				'show_tutor' => $this->input->post('chkTutorShow') ? $this->input->post('chkTutorShow') : 'N'
            );
        if($this->input->post('txtNewsID') != null && $this->input->post('txtNewsID') != '') {
        	$data['news_id'] = $this->input->post('txtNewsID');
        	$data['update_by'] = $this->session->userdata('username');
        	$key = $this->input->post('txtNewsID');
        	$this->NewsModel->updateNews($data);
        } else {
        	$data['create_by'] = $this->session->userdata('username');
			$key = $this->NewsModel->addNews($data);
        }
        $msg = array('msg' => '');
        $this->load->library('upload');
        	if(@$_FILES['fileUpload']['error'] == '0') 
        	{
				$config['upload_path'] = './data-image/news/';
				$config['allowed_types'] = 'gif|jpg|png|zip|pdf|doc|xls|ppt';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = $key.'-file';
				$this->upload->initialize($config);
				if ( ! $this->upload->do_upload('fileUpload'))
				{
					$msg['msg'] = "ไม่สามารถ upload ไฟล์แนบได้";			
				}
				else
				{
					$data = array('upload_data' => $this->upload->data());
					$Imgdata = array(
					   'news_id' => $key, 
		               'att_file' => $data['upload_data']['file_name']   
		            );
					$this->NewsModel->updateFilePath($Imgdata);
				}
        	}
        	if(@$_FILES['imageUpload']['error'] == '0') 
        	{
				$config['upload_path'] = './data-image/news/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = $key.'-image';
				$this->upload->initialize($config);
				
				if ( ! $this->upload->do_upload('imageUpload'))
				{
					$msg['msg'] .= "ไม่สามารถ upload ไฟล์รูปภาพได้";			
				}
				else
				{
					$data = array('upload_data' => $this->upload->data());
					$Imgdata = array(
					   'news_id' => $key, 
		               'att_image' => $data['upload_data']['file_name']   
		            );
					$this->NewsModel->updateImagePath($Imgdata);
				}
        	}
        	if($msg['msg'] == '') 
        	{
        		$msg['msg'] = "บันทึกข้อมูลเรียบร้อย";
        	} 
		
		$data = $this->NewsModel->getNewsByID($key);		
		$this->load->view('header');
		$this->load->view('news-detail-form', $data);
		$this->load->view('footer', $msg);
	}
}

